<script setup lang="ts">
import userAvatarName from './components/userAvatarName.vue'
import userWelfare from './components/userWelfare.vue'
import userFeature from './components/userFeature.vue'
import { ref, nextTick, inject } from 'vue'
import { useDeviceStore, usePagesMachineIdStore, useMemberStore, useLocationStore } from '@/stores'
import { onShow, onLoad } from '@dcloudio/uni-app'
import { getExternalSkipMiniApp, Toast } from '@/utils/tools'
const { loginPromise } = <{ loginPromise: Promise<unknown> }>inject('globalData')

const deviceStore = useDeviceStore()
const locationStore = useLocationStore()

/**
 *  popupRef ref获取组件实例
 */
const popupRef = ref()

import useMiniShare from '@/hooks/useMiniShare'
const { isShareFriends, setupOnShareAppMessage, onShareAppMessage } = useMiniShare()

isShareFriends()
onShareAppMessage
onShow(() => {
  const shareParams = {
    path: `/pages/my/my?machineId=${deviceStore.deviceInfo?.machineId}&machineName=${deviceStore.deviceInfo?.machineName}&positionAddr=${deviceStore.deviceInfo?.positionAddr}&businessStatus=${deviceStore.deviceInfo?.businessStatus}&saleBeginTime=${deviceStore.deviceInfo?.saleBeginTime}&saleEndTime=${deviceStore.deviceInfo?.saleEndTime}`,
    title: deviceStore.deviceInfo?.machineName,
  }
  setupOnShareAppMessage(shareParams)
})

const pagesMachineId = usePagesMachineIdStore()
const memberStore = useMemberStore()

import useQuery from '@/hooks/useQuery'
const { getQueryParams } = useQuery()

/** 获取设备列表信息 */
const getspotList = async () => {
  try {
    const res = await getMachineDataApi({
      machineId: pagesMachineId.pagesMachineId || null,
      clientId: memberStore.userInfo?.id || '',
      longitude: locationStore.location!.longitude,
      latitude: locationStore.location!.latitude,
      pageNum: 1,
      pageSize: 3,
    })
    let machineItem = res.data?.find((i: any) => pagesMachineId.pagesMachineId === i.machineId)
    machineItem ? deviceStore.setDeviceInfo(machineItem) : null
  } catch (error) {
    console.log('catch', error)
  }
}

onLoad((ev) => {
  console.log('onLoad', ev)
  ev?.machineId ? pagesMachineId.setPagesMachineId(ev.machineId) : null
  if (ev && ev.machineId) {
    loginPromise.then(() => {
      getspotList()
    })
  }
  // 用户是否扫优惠券登录
  const exportId = getQueryParams()?.exportId
  if (ev?.q) {
    nextTick(() => {
      if (!memberStore?.token) popupRef.value?.open() // 用户未登录，就弹出用户登录弹窗
    })

    getExternalSkipMiniApp(ev)
    getQRCodeCoupons()
  } else if (exportId) {
    getQRCodeCoupons()
  }
})
/**获取登录后自动获取二维码的优惠券 */
const getQRCodeCoupons = async () => {
  if (!memberStore.token) return
  const exportId = getQueryParams()?.exportId
  console.log('优惠券参数', exportId)
  const res = await postMarCouponExportIdApi(exportId)
  if (res.code === 200) {
    Toast('优惠券已到账')
  } else {
    Toast(res.msg)
  }
}
// 公共弹出框
import usePopup from '@/hooks/usePopup'
import { postMarCouponExportIdApi } from '@/services/my'
import { getMachineDataApi } from '@/services/home'
const { SkipPopupRef, receive, skipPopupData, handleClose } = usePopup()
</script>

<template>
  <view class="userInfo">
    <userAvatarName @open="popupRef.open()" />
  </view>
  <userWelfare @open="popupRef.open()" />
  <view class="box">
    <userFeature @open="popupRef.open()" class="userFeature" />
  </view>
  <BjsPopup ref="popupRef" />
  <BjsSkipPopup
    :img="skipPopupData.img"
    ref="SkipPopupRef"
    @receive="receive"
    @close="handleClose"
  />
</template>

<style lang="scss" scoped>
//
.userInfo {
  width: 100%;
  height: 424rpx;
  background: linear-gradient(180deg, #fbd57a 0%, #fda12a 100%);
}
.box {
  position: relative;
  .userFeature {
    position: absolute;
    top: -40rpx;
    width: 100%;
  }
}
</style>
<style>
page {
  background: #f5f5f5;
}
</style>
